<template>
    <UnitBaseInfo :opType="opType" :opUnitInfo="opUnitInfo" v-if="(opType==1||opType==3)&&opUnitInfo.id"/>

    <el-card class="w-full mt-[10px]">
        <CardHead> 年度统计记录 </CardHead>
        
        <el-table v-loading="loading" :data="educationStatList">
            <el-table-column label="统计年度" align="center" prop="statYear" width="100">
                <template #default="scope">
                <dict-tag :options="yearOptions" :value="scope.row.statYear"/>
                </template>
            </el-table-column>
            <el-table-column label="归属统计" align="center" prop="subjectClassId" width="100">
                <template #default="scope">
                <dict-tag :options="cfg_category_jybtjgs" :value="scope.row.subjectClassId" :showSpan="true"/>
                </template>
            </el-table-column>
            <el-table-column label="机构类型" :show-overflow-tooltip="true" align="center" prop="unitType">
                <template #default="scope">
                <dict-tag :options="cfg_category_jgxz" :value="scope.row.unitType" v-if="scope.row.subjectClassId === '2'" :showSpan="true"/>
                <dict-tag :options="cfg_category_jglx_flat" :value="scope.row.unitType" v-else-if="scope.row.subjectClassId === '1'" :showSpan="true" showSep="，"/>
                </template>
            </el-table-column>
            <el-table-column label="国民经济行业" :show-overflow-tooltip="true" align="center" prop="gmjjhy">
                <template #default="scope">
                <dict-tag :options="cfg_category_gmjjhy_flat" :value="scope.row.gmjjhy" :showSpan="true"/>
                </template>
            </el-table-column>
            <el-table-column label="学科分类" :show-overflow-tooltip="true" align="center" prop="subjectId">
                <template #default="scope">
                <dict-tag :options="cfg_category_yjxk" :value="scope.row.subjectId" :showSpan="true"/>
                </template>
            </el-table-column>
            <el-table-column label="高级职称" align="center" prop="seniorNum" width="100"/>
            <el-table-column label="中级职称" align="center" prop="intermediateNum" width="100"/>
            <el-table-column label="初级职称" align="center" prop="primaryNum" width="100"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="opType==1&&opUnitInfo.id">
                <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['research:educationStat:edit']">编辑</el-button>
                <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['research:educationStat:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
            
        <pagination
        v-show="total>0&&false"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
        />
    </el-card>

    <EducationStatEdit ref='educationStatEditCom' :title="title" :opType="opType" :opUnitId="opUnitInfo.id" :educationStatId="educationStatId" @educationStatAddSuccess="onEducationStatAddSuccess" @educationStatUpdateSuccess="onEducationStatUpdateSuccess" v-if="refreshFlag && props.opType === 1"/>
</template>

<script setup name="EducationStat">
import useUserStore from '@/store/modules/user';
import CardHead from "./CardHead.vue";
import UnitBaseInfo from "./UnitBaseInfo.vue";
import EducationStatEdit from "./EducationStatEdit.vue";
import { getOverPastYearOptions, getCategoryData, getFlatTree } from "@/api/research/unit";
import { listEducationStat, getEducationStat, delEducationStat, addEducationStat, updateEducationStat } from "@/api/research/educationStat";

const { proxy } = getCurrentInstance();

const emit = defineEmits(['refreshUnitList']);  
const props = defineProps({
  opType: {  
    type: Number,
    required: true,
    default: 0      // 默认值  0:add 1:edit 2:check 3:view
  },
  opUnitInfo: {  
    type: Object,
    required: false,
  }
}) 

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 1000,
    researchUnitId: props.opUnitInfo.id,
  },
});

const { queryParams } = toRefs(data);

const cfg_category_jybtjgs = ref([]);
const cfg_category_jgxz = ref([]);
const cfg_category_jglx = ref([]);
const cfg_category_jglx_flat = ref([]);
const cfg_category_gmjjhy = ref([]);
const cfg_category_gmjjhy_flat = ref([]);
const cfg_category_yjxk = ref([]);

const yearOptions = ref([]);

const educationStatEditCom = ref(null);
const refreshFlag = ref(true);

const educationStatList = ref([]);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const educationStatId = ref(null);

onMounted(() => {
  yearOptions.value = getOverPastYearOptions(10,true);
  getCategoryData(cfg_category_jybtjgs, {bizName:"教育部统计归属"});
  getCategoryData(cfg_category_gmjjhy, {bizName: "国民经济行业"},false,()=>getFlatTree(cfg_category_gmjjhy.value, cfg_category_gmjjhy_flat.value));                           //国民经济行业      树状
  getCategoryData(cfg_category_yjxk, {bizName: "一级学科"}, true);                          //一级学科
  getCategoryData(cfg_category_jgxz, {bizName: "平台_教育部统计机构类型_科技类"});            //科技类    机构类型          单选
  getCategoryData(cfg_category_jglx, {bizName: "平台_教育部统计机构类型_社科类"},false,()=>getFlatTree(cfg_category_jglx.value, cfg_category_jglx_flat.value));            //社科类    机构类型          树状
  handleAdd();
})

function refreshCom() {
  refreshFlag.value = false;
  setTimeout(() => refreshFlag.value = true, 0);
}

function getList() {
  loading.value = true;
  listEducationStat(queryParams.value).then(response => {
    educationStatList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleAdd() {
  educationStatId.value = null;
  title.value = "统计信息新增";
  refreshCom();
}

function handleUpdate(row) {
  educationStatId.value = row.id || ids.value;
  title.value = "统计信息编辑";
  refreshCom();
}

function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('您确定要删除吗?').then(function() {
    return delEducationStat(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

function onSaveSuccess(params) {
    getList();
    handleAdd();
    // proxy.$modal.confirm('是否继续操作？').then(function() {
    //     handleAdd();
    // }).catch(() => {
    //     cancel();
    //     emit('refreshUnitList');
    // });
}

function onEducationStatAddSuccess(params) {
    onSaveSuccess();
}

function onEducationStatUpdateSuccess(params) {
    onSaveSuccess();
}

function save() {
  educationStatEditCom.value.save();
}

function cancel() {
  educationStatEditCom.value.cancel();
}

defineExpose({save,cancel});
getList();
</script>

<style scoped lang="scss">
@import url("../index.scss");
:deep(.cell.el-tooltip){
  white-space: normal !important;
}
</style>
